package com.ruoyi.common.utils;

import cn.hutool.core.util.ZipUtil;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.zip.GZIPInputStream;
import java.util.zip.GZIPOutputStream;

/**
 * @ProjectName: ReportQuery
 * @Package: com.ruoyi.framework
 * @ClassName: Test
 * @Description: java类作用描述
 * @Author: Zhujj
 * @CreateDate: 2020/01/10 20:40
 * @Version: 1.0
 */
public class ZipUtils {

    public static void main(String[] args) {
        String token = "eyJhbGciOiJIUzUxMiJ9.eyJsb2dpbl91c2VyX2tleSI6IjM0ZWY3YTk3LTFmNzctNDEwMS1hOTFmLWJjMDU4NzlmNWE1MyJ9.MTU9TmSrRdq3MSpQ0nUxbdSIPHZ6GziPiBpbbPSk62u5FQna6qLp34eMwNE2bgH-lIUoWHvMmzpTHpoPoupq2A";
       try {
           String newtoken = compress(token, "ISO-8859-1");
           System.out.println(newtoken);
           String unzipToken = uncompress(newtoken, "ISO-8859-1");
           System.out.println(unzipToken);
       }catch (Exception e){

       }
    }
    public static String compress(String str,String inEncoding) throws IOException {
        if (str == null || str.length() == 0) {
            return str;
        }
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        GZIPOutputStream gzip = new GZIPOutputStream(out);
        gzip.write(str.getBytes(inEncoding));
        gzip.close();
        return out.toString("ISO-8859-1");

    }

    public static String uncompress(String str,String outEncoding) throws IOException {
        if (str == null || str.length() == 0) {
            return str;
        }
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        ByteArrayInputStream in = new ByteArrayInputStream(str.getBytes("ISO-8859-1"));
        GZIPInputStream gunzip = new GZIPInputStream(in);
        byte[] buffer = new byte[256];
        int n;
        while ((n = gunzip.read(buffer)) >= 0) {
            out.write(buffer, 0, n);
        }
        return out.toString(outEncoding);
    }
}
